package com.jeff.android.lab.module.anim.path;

import android.content.Context;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.jeff.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: dingfeng
 * @date: 2020-09-14
 * @since: 1.0.0
 **/
public class CurveView extends View {

    private int[] mYData = new int[]{129, 188, 30, 88, 55, 200, 108};
//    private int[] mYData = new int[]{1, 2, 0, 50, 3, 20, 2};
    private int mYAverageValue;

    // 需要绘制的点的数据
    private List<PointF> mPointData = new ArrayList<>();

    private Paint mPaint; // 曲线
    private Path mPath;

    private Paint mPaintBackground;
    private Path mPathBackground;

    private int mWidth = 0;
    private int mHeight = 0;

    private int mXSpace = 0; // x 轴间距

    public CurveView(Context context) {
        super(context);
        init();
    }

    public CurveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CurveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
//        mPaint.setColor(Color.parseColor("#00aff9"));
        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(6);

//        LinearGradient lg = new LinearGradient(0, 0, mWidth, mHeight,
//                Color.parseColor("#03ECFF"), Color.parseColor("#057AFF"),
//                Shader.TileMode.CLAMP);
//        mPaint.setShader(lg);

        mPath = new Path();

        mPaintBackground = new Paint();
        mPaintBackground.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintBackground.setStrokeWidth(0.1f);

        mPathBackground = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w - 50;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCurve(canvas);

        drawTodayPoint(canvas);

        drawText(canvas);
    }

    private void drawCurve(Canvas canvas) {
        mXSpace = mWidth / (mYData.length - 1);
        LogUtil.d("dingfeng",  "间距：" + mXSpace);

        int sum = 0;
        for (int yValue : mYData) {
            sum += yValue;
        }
        mYAverageValue = sum / mYData.length;

        int pix = mHeight / (2 * mYAverageValue);


        for (int i = 0; i < mYData.length; i++) {
            PointF pointF = new PointF(mXSpace * i, mHeight - pix * mYData[i]);
            mPointData.add(pointF);


            LogUtil.d("dingfeng", pointF.x + " :: " + pointF.y);
        }

        LinearGradient lg = new LinearGradient(0, mPointData.get(0).y, mWidth, mPointData.get(mYData.length - 1).y,
                Color.parseColor("#03ECFF"), Color.parseColor("#057AFF"),
                Shader.TileMode.CLAMP);
        mPaint.setShader(lg);

//        LinearGradient bgLg = new LinearGradient(0, mPointData.get(0).y, mWidth, mPointData.get(mYData.length - 1).y,
//                Color.parseColor("#FFFFFF"), Color.parseColor("#03ECFF"),
//                Shader.TileMode.CLAMP);
//        mPaintBackground.setShader(bgLg);
        mPaintBackground.setColor(Color.parseColor("#6703ECFF"));

        PointF startP;
        PointF endP;

        // 初始位置
        mPath.moveTo(mPointData.get(0).x, mPointData.get(0).y);
        mPathBackground.moveTo(mPointData.get(0).x, mPointData.get(0).y);



        Paint paintBg = new Paint();
        paintBg.setStyle(Paint.Style.FILL_AND_STROKE);
//        paintBg.setStrokeWidth(0.1f);
//                LinearGradient bgLg = new LinearGradient(mPointData.get(mPointData.size() - 1).x, mHeight, mPointData.get(0).x, mPointData.get(0).y,
//                Color.parseColor("#FFFFFF"), Color.parseColor("#03ECFF"),
//                Shader.TileMode.CLAMP);
//        paintBg.setShader(bgLg);
        paintBg.setColor(Color.WHITE);

        Path path = new Path();
        path.moveTo(mPointData.get(0).x, mPointData.get(0).y);

        for (int i = 0; i < mPointData.size() - 1; i++) {
            startP = mPointData.get(i);
            endP = mPointData.get(i + 1);

            float wt = (startP.x + endP.x) / 2;
            PointF p3 = new PointF();
            PointF p4 = new PointF();
            p3.y = startP.y;
            p3.x = wt;
            p4.y = endP.y;
            p4.x = wt;

            mPath.cubicTo(p3.x, p3.y, p4.x, p4.y, endP.x, endP.y);

            if (i == mPointData.size() - 2) {
                mPathBackground.cubicTo(p3.x, p3.y, p4.x, p4.y, endP.x + 12, endP.y + 6);

                path.cubicTo(p3.x, p3.y, p4.x, p4.y, endP.x + 12, endP.y);
            } else {
                mPathBackground.cubicTo(p3.x, p3.y, p4.x, p4.y, endP.x, endP.y);

                path.cubicTo(p3.x, p3.y, p4.x, p4.y, endP.x, endP.y);
            }



        }

        // 绘制背景
//        mPathBackground.lineTo(mPointData.get(mPointData.size() - 1).x + 12, mHeight);
//        mPathBackground.lineTo(0, mHeight);
//        mPathBackground.close();
//
//        canvas.drawPath(mPathBackground, mPaintBackground);

        // test

        path.lineTo(mPointData.get(mPointData.size() - 1).x + 12, 0);
        path.lineTo(0, 0);
        path.close();

        canvas.drawPath(path, paintBg);



        // 绘制曲线
        canvas.drawPath(mPath, mPaint);
    }

    private void drawTodayPoint(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#057AFF"));
        paint.setStrokeWidth(6);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(mPointData.get(mPointData.size() - 1).x + 12, mPointData.get(mPointData.size() - 1).y, 12, paint);


        Paint paint1 = new Paint();
        paint1.setColor(Color.WHITE);
//        paint1.setStrokeWidth(6);
        paint1.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(mPointData.get(mPointData.size() - 1).x + 12, mPointData.get(mPointData.size() - 1).y, 8, paint1);
    }

    private void drawText(Canvas canvas) {

        // 画背景
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#057AFF"));
        paint.setStrokeWidth(6);
        paint.setStyle(Paint.Style.FILL);

        float lastX = mPointData.get(mPointData.size() - 1).x;
        float lastY = mPointData.get(mPointData.size() - 1).y;

        RectF rectF = new RectF(lastX - 50, lastY - 80, lastX + 50, lastY - 30);
//        canvas.drawRect(rectF, paint);
        canvas.drawRoundRect(rectF, 10, 10, paint);

        //画文字
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.WHITE);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(20);
        String value = String.valueOf(mYData[mYData.length - 1]);
        canvas.drawText(value, lastX - getTextWidth(textPaint, value) / 2, lastY - 55 + getTextHeight(textPaint, value) / 2, textPaint);
    }


    /**
     * 获取文字的宽度
     *
     * @param paint
     * @param text
     * @return
     */
    private int getTextWidth(Paint paint, String text) {
        return (int) paint.measureText(text);
    }

    /**
     * 获取文字的高度
     *
     * @param paint
     * @param text
     * @return
     */
    private int getTextHeight(Paint paint, String text) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

}
